/**
 * 
 */
package com.conan.zone.system.web.action;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.WebUtils;

import com.conan.zone.exception.SystemException;
import com.conan.zone.framework.web.action.AbstractAction;
import com.conan.zone.system.exception.LoginValidateErrorException;
import com.conan.zone.system.model.User;
import com.conan.zone.system.service.ILoginService;
import com.conan.zone.util.CommonUtil;
import com.conan.zone.util.ResponseUtils;

/**
 * @author Conan
 *
 */
@Controller
public class LoginAction extends AbstractAction{
	
	@Resource
	@Qualifier("loginServiceImpl")
	private ILoginService loginService;
	private final static String USER_SESSION_NAME = "userSession";
	public final static String LOGIN_COOKIE_UID = "UID";
	/**
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value="/toLogin")
	public String toLoginPage(HttpServletRequest request,HttpServletResponse response,User user,Model model){
		Cookie cookie = new Cookie(LOGIN_COOKIE_UID,CommonUtil.randomString());
		response.addCookie(cookie);
		return "system/login";
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value="/login")
	public String login(HttpServletRequest request,HttpServletResponse response,User user,Model model){
		try{
			User u = loginService.userLogin(user);
			if(u != null){
				setUserSession(request, user);
			}
			return "system/main";
		}catch(LoginValidateErrorException e){
			e.printStackTrace();
			ResponseUtils.renderHtml(response, e.getMessage());
		}catch(SystemException e){
			e.printStackTrace();
			ResponseUtils.renderHtml(response, "error");
		}
		return null;
	}
	
	@RequestMapping(method=RequestMethod.GET,value="/logout")
	public String logout(HttpServletRequest request,HttpServletResponse response,User user,Model model){
		try{
			removeUserSession(request);
			return "redirect:toLogin.html";
		}catch(LoginValidateErrorException e){
			e.printStackTrace();
			ResponseUtils.renderHtml(response, e.getMessage());
		}catch(SystemException e){
			e.printStackTrace();
			ResponseUtils.renderHtml(response, "error");
		}
		return null;
	}
	
	/**
	 * 
	 * @param request
	 */
	public static User getUserSession(HttpServletRequest request){
		String name = userSessionName(request);
		if(StringUtils.isNotBlank(name)){
			return (User)WebUtils.getSessionAttribute(request, name);
		}
		return null;
	}
	/**
	 * 
	 * @param request
	 * @param user
	 */
	public static void setUserSession(HttpServletRequest request,User user){
		String name = userSessionName(request);
		if(StringUtils.isNotBlank(name)){
			request.getSession().setAttribute(name, user);
		}
	}
	
	/**
	 * 
	 * @param request
	 */
	public static void removeUserSession(HttpServletRequest request){
		String name = userSessionName(request);
		request.getSession().removeAttribute(name);
	}
	
	/**
	 * 
	 * @param request
	 * @return
	 */
	public static String userSessionName(HttpServletRequest request){
		Cookie cookie = WebUtils.getCookie(request, LOGIN_COOKIE_UID);
		if(cookie!=null){
			return USER_SESSION_NAME+cookie.getValue();
		}
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value="/menu")
	public String menu(){
		return "system/menu";
	}
	/**
	 * 
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value="/welcome")
	public String welcome(){
		return "system/welcome";
	}
}
